package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;


    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        //根据商品分类id查询
        SpecGroup record = new SpecGroup();
        record.setCid(id);
        List<SpecGroup> list = groupMapper.select(record);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> queryParams(Long gid, Long cid, Boolean searching) {
        SpecParam param = new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setSearching(searching);
        List<SpecParam> list = paramMapper.select(param);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }

    public List<SpecGroupDTO> querySpecsByCid(Long cid) {
        //查询规格组
        List<SpecGroupDTO> specGroupDTOS = queryGroupByCategory(cid);
        //查询规格参数
        List<SpecParamDTO> params = queryParams(null, cid, null);

        //把参数分组，key是groupId，值是group下的所有Param的集合
        Map<Long, List<SpecParamDTO>> map = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //把分好的组set到Group
        for (SpecGroupDTO groupDTO : specGroupDTOS) {
            groupDTO.setParams(map.get(groupDTO.getId()));
        }

/*        //把参数分组，key是groupId，值是group下的所有Param的集合
        Map<Long, List<SpecParamDTO>> map = new HashMap<>();
        for (SpecParamDTO param : params) {
            //判断当前这个param所属的group是否已经在map中存在了
            if (!map.containsKey(param.getGroupId())) {
                //不存在，说明是当前group的第一个参数，那么我们新增一个List进去
                map.put(param.getGroupId(), new ArrayList<>());
            }
            //存在，则取出group下的List，然后把param加进去
            map.get(param.getGroupId()).add(param);
        }

        //把分好的组set到Group
        for (SpecGroupDTO groupDTO : specGroupDTOS) {
            groupDTO.setParams(map.get(groupDTO.getId()));
        }*/

        /*for (SpecParamDTO param : params) {
            Long groupId = param.getGroupId();
            for (SpecGroupDTO groupDTO : specGroupDTOS) {
                List<SpecParamDTO> list = groupDTO.getParams();
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.add(param);
            }
        }*/

        /*for (SpecGroupDTO groupDTO : specGroupDTOS) {
            Long groupId = groupDTO.getId();
            //根据组id查询组内参数
            SpecParam param = new SpecParam();
            param.setGroupId(groupId);
            List<SpecParam> params = paramMapper.select(param);
            if (CollectionUtils.isEmpty(params)){
                throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
            }
            groupDTO.setParams(BeanHelper.copyWithCollection(params, SpecParamDTO.class));
        }*/
        return specGroupDTOS;
    }
}
